<?php


define('DUB_AUTHOR_ID', 'dub_author');
define('DUB_AUTHOR_ARTICLE_COUNT_ID', 'dub_author_article_count');


// export $GLOBALS['dub_author']
// uri_parse att: figures out who the current page is talking about
// 		i.e. /team/jamie-wilkinson -> jamie
// 		this means the author login has to be their first name!!
function dub_author_export($atts)
{
	extract($atts);

	// fallback to this article's author
	$parsed_author = $GLOBALS['pretext']['author'];

	// cool, parse from URI
	if($uri_parse) {
		$ex = explode('/', $GLOBALS['req']);
		$ez = explode('-', $ex[2]);
		$parsed_author = $ez[0];
	}
	
	// export
	$GLOBALS[DUB_AUTHOR_ID] = $parsed_author;

	return;
}

// try a variety of places author is stored,
// including our own "parse the URL" storage method
// from above
function dub_author($atts = '')
{
	global $pretext;
	if($GLOBALS[DUB_AUTHOR_ID])
		return $GLOBALS[DUB_AUTHOR_ID];
	else if($pretext['author'])
		return $pretext['author'];
	else if($GLOBALS['pretext']['author'])
		return $GLOBALS['pretext']['author'];

	// :(
	return false;
}



// fetch all articles by said author
/*	@atts
		author
		form
		limit
		other std ish
*/
function dub_author_articles($atts)
{
	extract($atts);

	$out = "dub_author_articles(): NOT IMPLEMENTED YET.";
// use below instead
//	'<txp:php>';
// print '<txp:article_custom section="blog" form="article_simple" author="'.$GLOBALS['dub_author'].'" limit="100" />';
//	</txp:php>
	return $out; 
}

// fetch # of articles by said author
/*
	@atts
		author (required)
		section
		category [TODO]
*/		
function dub_author_article_count($atts)
{
	// cache the article count on this page...
	if(empty($GLOBALS[DUB_AUTHOR_ARTICLE_COUNT_ID])) 
	{
		//print "<!-- dub_author_article_count: empty, fetching... -->";
		extract(lAtts(array(
			'author' => $GLOBALS[DUB_AUTHOR_ID],
			'section' => ''
		), $atts));
	
		$where = "AuthorID = '$author'";
		if($section)
			$where .= " AND section='$section'";
		if($category) {
			// TODO
		}
		
		$row = safe_row('count(*)', 'textpattern', $where);
		$out = $row['count(*)'];
		$GLOBALS[DUB_AUTHOR_ARTICLE_COUNT_ID] = $out;
		return $out;
	}
	// it's in GLOBALS, return that
	else {
		//print "<!-- dub_author_article_count: returning cache -->";
		return $GLOBALS[DUB_AUTHOR_ARTICLE_COUNT_ID];
	}
}

function dub_author_if_articles($atts, $thing)
{
	extract($atts);
	$count = dub_author_article_count($atts);
	return parse(EvalElse($thing, ($count > 0)));
}

function dub_list_authors($atts)
{
	extract(lAtts(array(
	  	'link' => 'y',
	  	'link_to_section' => 'author',
		'privs' => '',
		'if_articles' => 'y',
		'thickbox_author_images' => ''
	),$atts));
	
	$out = $link_pfx = $link_sfx = '';

	if($privs)
		$where .= 'privs = '.$privs.'';
	$authors = safe_rows( 'name, RealName, privs', 'txp_users', ($where ? $where : '1=1') ); // FIXME * is bad
//	$out .= 'authors<br /><pre>'.print_r($authors, true).'</pre>';
	
	$img_out = ''; // for author images
	for($i = 0; $i < count($authors); $i++) {
		if($link && $link_to_section) {
			if( !empty($thickbox_author_images) ) {
				// fetch images...
				$images = safe_rows('*','txp_image','author="'.$authors[$i]['name'].'"');
				$done_first = false;
//				$out .= 'images<br /><pre>'.print_r($images, true).'</pre>';
				
				if(count($images)) {
					foreach($images as $img) {
						// link pfx = link to 1st img
						if(!$done_first) {
							$link_pfx = '<a href="/images/'.$img['id'].$img['ext'].'" class="thickbox" rel="'.$authors[$i]['name'].'">';
							$link_sfx = '</a>';
							$done_first = true;
							next;
						}
						// add others in a a list to be hidden
						else {
							$img_out .= '<a href="/images/'.$img['id'].$img['ext'].'" class="thickbox" rel="'.$authors[$i]['name'].'">2</a>';
						}
					}
				}
				else { // no images, make sure no link
					$link_pfx = '';
					$link_sfx = '';
				}
			}
			else {
				$link_pfx = '<a href="/'.$link_to_section.'/'.$authors[$i]['name'].'" '.(empty($thickbox_author_images) ? '' : 'class="thickbox" rel="'.$authors[$i]['name'].'"').'>';
				$link_sfx = '</a>';
			}


		}
		
		$out .= $link_pfx.$authors[$i]['RealName'].$link_sfx;
		
		if(!empty($authors[$i + 1])) {
			$out .= ', ';
		}
	}
	
	// thickbox stuff
	$out .= '<div style="display: none">';
	$out .= $img_out;
	$out .= '</div>';
	
	return $out;
}

?>